import { WxOpenDataCommands } from "./WxOpenDataConsts";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Sprite)
    imgRank: cc.Sprite = null;

    rankTexture: cc.Texture2D;


    // LIFE-CYCLE CALLBACKS:
    onEnable () {
        if(cc.sys.platform === cc.sys.WECHAT_GAME)
        {
            const sharedCanvas = wx.getOpenDataContext().canvas;
            sharedCanvas.width = this.imgRank.node.width;
            sharedCanvas.height = this.imgRank.node.height;
            this.rankTexture = new cc.Texture2D();

            this.showRankList();
        }
    }

    update (dt) {   
        if(cc.sys.platform === cc.sys.WECHAT_GAME)
        {    
            var openDataContext = wx.getOpenDataContext();
            var sharedCanvas = openDataContext.canvas;
            this.rankTexture.initWithElement(sharedCanvas);
            this.rankTexture.handleLoadedTexture();
            this.imgRank.spriteFrame = new cc.SpriteFrame(this.rankTexture);
        }
    }
    

    onDisable() {
        this.rankTexture = null;
    }

    showRankList()
    {
        if(cc.sys.platform === cc.sys.WECHAT_GAME)
        {
            wx.getOpenDataContext().postMessage({
                action: WxOpenDataCommands.FetchFriendData
            });
        }
    }
}
